<?php

function CABLE_adminInit(&$connector)
{
	global $cable_admin_plugin;
	
	/* initialize our plugin global */
	$cable_admin_plugin = array();
	
	/* keep a link to our connector */
	$cable_admin_plugin[connector] = $connector;
	
	/* add the necessary css reference to the head of the template */
	$connector->template->AppendInsert('Head','<link type="text/css" href="{I CABLE-Base.url /}css/cable_admin/admin.css" rel="stylesheet" />');
	
	/* load admin menu template */
	$cable_admin_plugin[menu_template] = new CABLEtemplate($connector->config,$connector->db,'cable_admin','admin_0');
	if ($cable_admin_plugin[menu_template]->error !== false)
		return 'Error retrieving cable_admin.admin_0 template.';
	
	/* load the necessary action template from the database */
	switch (urldecode($_REQUEST['action']))
	{
		case 'new_entry':
		case 'delete_entry':
		case 'edit_entry':
		case 'list_entries':
			$cable_admin_plugin[action_template] = new CABLEtemplate($connector->config,$connector->db,'cable_admin','admin_1');
			if ($cable_admin_plugin[action_template]->error !== false)
				return 'Error retrieving cable_admin.admin_1 template.';
			break;
		case 'list_files':
		case 'upload_files':
			$cable_admin_plugin[action_template] = new CABLEtemplate($connector->config,$connector->db,'cable_admin','admin_2');
			if ($cable_admin_plugin[action_template]->error !== false)
				return 'Error retrieving cable_admin.admin_2 template.';
			break;
		case 'edit_plugins':
		case 'edit_themes':
		case 'clear_caches':
		case 'general_settings':
			$cable_admin_plugin[action_template] = new CABLEtemplate($connector->config,$connector->db,'cable_admin','admin_3');
			if ($cable_admin_plugin[action_template]->error !== false)
				return 'Error retrieving cable_admin.admin_3 template.';
			break;
		case 'edit_account':
		case 'ban_user':
		case 'list_users':
		case 'login':
		case 'logout':
		case 'sign_up':
		case 'lost_password':
		default:
			$cable_admin_plugin[action_template] = new CABLEtemplate($connector->config,$connector->db,'cable_admin','admin_4');
			if ($cable_admin_plugin[action_template]->error !== false)
				return 'Error retrieving cable_admin.admin_4 template.';
			break;
	}

	return true;
}

function CABLE_adminMenu($connector)
{
	global $cable_admin_plugin;

	/* initialize menu array */
	$entry_menu = array();
	if ($connector->user->values[adminlevel] > 0)
	{
		$entry_menu['New Entry'] = 'new_entry';
		$entry_menu['Delete Entry'] = 'delete_entry';
		$entry_menu['List Entries'] = 'list_entries';
	}
	
	$file_menu = array();
	if ($connector->user->values[adminlevel] > 0)
	{
		$file_menu['Upload New'] = 'upload_files';
		$file_menu['List All'] = 'list_files';

	}
	
	$settings_menu = array();
	if ($connector->user->values[adminlevel] == 2)
	{
		$settings_menu['General Settings'] = 'general_settings';
		$settings_menu['Load/Config Plugins'] = 'edit_plugins';
		$settings_menu['Add/Edit Themes'] = 'edit_themes';
		$settings_menu['Clear Caches'] = 'clear_caches';
	}
	
	$account_menu = array('Help' => 'help');
	if ($connector->user->values[adminlevel] > 0)
		$account_menu['List Users'] = 'list_users';
	if ($connector->user->logged_in)
	{
		$account_menu['Edit Account'] = 'edit_account';
		$account_menu['Log Out'] = 'logout';
	}
	else
		$account_menu['Log In'] = 'login';
	
	$cable_admin_plugin[menu] = array(
	'Entry' => $entry_menu,
	'File Editor' => $file_menu,
	'Settings' => $settings_menu,
	'Account' => $account_menu);
	
	/* get menu subtemplates */
	$submenu_template = $cable_admin_plugin[menu_template]->GetChild('CABLE-Submenu');
	if ($submenu_template === false)
		return 'Error loading menus.';
	$option_template = $submenu_template->GetChild('CABLE-Option');
	if ($option_template === false)
		return 'Error loading menu template.';
	
	/* build menu */
	foreach($cable_admin_plugin[menu] as $menu_name => $menu_contents)
	{
		$submenu_template->Reset();
		$submenu_template->SetBool('CABLE-Submenu',(count($menu_contents) > 0));
		foreach($menu_contents as $option_name => $option_action)
		{
			$option_template->Reset();
			$option_template->ReplaceInsert('CABLE-Option.name',$option_name);
			$option_template->ReplaceInsert('CABLE-Option.action',$option_action);
			$submenu_template->AppendInsert('CABLE-Options',$option_template->template);
		}
		$submenu_template->ReplaceInsert('CABLE-Submenu.name', $menu_name);
		$submenu_template->ReplaceInsert('CABLE-Submenu.action',strtolower(str_replace(' ','_',$menu_name)));
		$cable_admin_plugin[menu_template]->AppendInsert('CABLE-Submenus',$submenu_template->template);
	}
	
	return true;
}

function CABLE_adminAuthors($author)
{
	global $cable_admin_plugin;
	
	/* function fills out the current action template with available authors */
	
	/* retrieve author template from the current action template */
	$author_template = $cable_admin_plugin[action_template]->GetChild('CABLE-Author');
	if ($user_template === false)
		return 'Error retrieving file author template.';
	
	/* if user isn't superuser, restrict abilities to only his username */
	$table = "cable_".$cable_admin_plugin[connector]->config['TablePrefix']."_users";
	if ($cable_admin_plugin[connector]->user->values[adminlevel] < 2)
	{
		$author = $cable_admin_plugin[connector]->user->values[username];
		$query = "SELECT * FROM $table WHERE (username = '$author')";
	}
	else
		$query = "SELECT * FROM $table WHERE (adminlevel > 0)";

	/* insert user data query results into template */	
	if(($result = mysql_query($query,$cable_admin_plugin[connector]->db_link)) !== false)
	{
		while(($array = mysql_fetch_assoc($result)) !== false)
		{
			foreach($array as $field_name => $field_value)
				$author_template->ReplaceInsert('CABLE-Author.'.$field_name,$field_value);

			$author_template->SetBool('CABLE-Author.selected',$array[username] == $author);
			
			$cable_admin_plugin[action_template]->AppendInsert('CABLE-Authors',$author_template->template);
			$author_template->Reset();
		}
	}
	else
		return 'Error accessing user database table';
	
	return true;
}
?>